package cn.edu.hstc.wm.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.bean.Good;
import cn.edu.hstc.wm.bean.Store;
import cn.edu.hstc.wm.ui.base.BaseActivity;
import cn.edu.hstc.wm.ui.fragment.GoOrderDialog;
import cn.edu.hstc.wm.ui.fragment.StoreItemMenu;
import cn.edu.hstc.wm.widget.ActionItem;
import cn.edu.hstc.wm.widget.CustomToast;
import cn.edu.hstc.wm.widget.QuickAction;
import cn.edu.hstc.wm.widget.QuickAction.OnActionItemClickListener;

/**
 * 去下单页面
 * 
 */
public class GoOrder extends FragmentActivity {
	public static final String TAG = "GoOrder";

	public static final String GO_ORDER_STORE = "GO_ORDER_STORE";
	public static final String GO_ORDER_GOODS_LIST = "GO_ORDER_GOODS_LIST";

	/**
	 * pop action item 标记
	 */
	private static final int ID_ADD = 1;
	private static final int ID_DECREASE = 2;
	private static final int ID_CLEAR = 3;

	/**
	 * pop window view
	 */
	QuickAction mQuickAction;

	/**
	 * 当前操作的item
	 */
	private int mCurrentItem = 0;

	ListView listView;
	SimpleAdapter adapter;
	TextView totalPrice;
	TextView totalCount;
	Store mStore;
	HashSet<Good> list;

	String[] from = { "title", "count", "total_price" };
	int[] to = { R.id.tv_go_order_list_item_title, R.id.tv_go_order_list_item_count, R.id.tv_go_order_list_item_total_price };

	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.go_order);

		initView();
		initData();
		initPopView();
	}

	private void initView() {

		listView = (ListView) findViewById(R.id.listView_go_order);
		totalPrice = (TextView) findViewById(R.id.tv_go_order_total_price);
		totalCount = (TextView) findViewById(R.id.tv_go_order_total_count);

		listView.setOnItemLongClickListener(new ItemLongClickListener());
		listView.setOnItemClickListener(new ItemClickListener());
	}

	/**
	 * 初始化pop window
	 */
	private void initPopView() {
		ActionItem addItem = new ActionItem(ID_ADD, "添加", getResources().getDrawable(android.R.drawable.ic_input_add));
		ActionItem decreaseItem = new ActionItem(ID_DECREASE, "减少", getResources().getDrawable(android.R.drawable.ic_menu_delete));
		ActionItem clearItem = new ActionItem(ID_CLEAR, "删除", getResources().getDrawable(android.R.drawable.ic_delete));

		mQuickAction = new QuickAction(this);
		mQuickAction.addActionItem(addItem);
		mQuickAction.addActionItem(decreaseItem);
		mQuickAction.addActionItem(clearItem);

		// 监听点击了某个后的动作
		mQuickAction.setOnActionItemClickListener(new OnActionItemClickListener() {

			@Override
			public void onItemClick(QuickAction source, int pos, int actionId) {
				// ActionItem item = source.getActionItem(pos);
				Iterator<Good> iterator = list.iterator();
				switch (actionId) {
				case ID_ADD:

					break;
				case ID_DECREASE:
					Log.d(TAG, "2");
					break;
				case ID_CLEAR:
					Log.d(TAG, "3");
					break;
				default:
					break;
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	private void initData() {
		mStore = (Store) getIntent().getSerializableExtra(StoreItemMenu.GO_ORDER_STORE);

		list = (HashSet<Good>) getIntent().getSerializableExtra(StoreItemMenu.GO_ORDER_GOODS_LIST);
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();

		Good good;
		Iterator<Good> iterator = list.iterator();

		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			if (iterator.hasNext()) {
				good = iterator.next();
				map.put("title", good.getName()); // 菜单名
				map.put("count", String.format(getString(R.string.tv_go_order_list_item_count), good.getSelectCount())); // 份数
				map.put("total_price", "￥" + good.getPrice() * good.getSelectCount()); // 总价
				Log.d(TAG, good.toString());
			}

			data.add(map);
		}

		adapter = new SimpleAdapter(this, data, R.layout.go_order_list_item, from, to);
		listView.setAdapter(adapter);

		// 总计
		totalCount.setText(getIntent().getIntExtra(StoreItemMenu.GO_ORDER_LIST_COUNT, 0) + " 份");
		totalPrice.setText("￥" + getIntent().getFloatExtra(StoreItemMenu.GO_ORDER_LIST_PRICE, 0) + "");
	}

	public void confirmOrder() {
		// TODO 提交数据了
		// 正在进行任务提示

		Intent intent = new Intent(this, StoreItemMenu.class);
		setResult(RESULT_OK, intent);

		// finish();
	}

	/**
	 * 点击返回页面
	 * 
	 * @param view
	 */
	public void back(View view) {
		CustomToast.make(this).setText("取消").show();
		setResult(RESULT_CANCELED);
		finish();
	}

	@Override
	public void onDestroy() {
		list.clear();
		super.onDestroy();
	}

	/**
	 * 在线下单
	 */
	public void onlineConfirmOrder(View view) {
		CustomToast.make(this).setText("正在开通在线下单功能中....").show();
		// TODO 提交数据了

		// 正在进行任务提示

		Intent intent = new Intent(this, StoreItemMenu.class);
		setResult(RESULT_OK, intent);

		// finish();
	}

	/**
	 * 电话/短信下单
	 */
	public void callOrSmsConfirmOrder(View view) {
		Bundle bundle = new Bundle();
		bundle.putSerializable(GO_ORDER_STORE, mStore);
		bundle.putSerializable(GO_ORDER_GOODS_LIST, list);
		GoOrderDialog.newInstance(bundle).show(getSupportFragmentManager(), "no title");

	}

	class ItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			mCurrentItem = position;
			// 显示操作窗口
			mQuickAction.show(view);
		}

	}

	class ItemLongClickListener implements android.widget.AdapterView.OnItemLongClickListener {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

			// 返回false,交给onClick监听处理
			return false;
		}

	}
}
